package com.java.util;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BindUtils
{

    private boolean bindEmptyMultipartFiles = true;

    /**
     * @return the bindEmptyMultipartFiles
     */
    public boolean isBindEmptyMultipartFiles()
    {
        return bindEmptyMultipartFiles;
    }

    /**
     * MultipartHttpServletRequest 로부터 request 정보를 map 컬렉션으로 담는다.
     * 
     * @param request
     * @return
     */
   /* private HttpMap getMultipartParameterMap( HttpServletRequest request )
    {
        HttpMap parameters = new RequestMap();
        String key = null;
        String[] value = null;

        for ( Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements(); )
        {
            key = enumeration.nextElement().toString();
            value = request.getParameterValues( key );
            if ( value == null || value.length == 1 )
            {
                parameters.put( key, value[0] );
            }
            else
            {
                parameters.put( key, value );
            }
        }

        MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) request;
        Map multipartFiles = mreq.getFileMap();
        for ( Iterator it = multipartFiles.entrySet().iterator(); it.hasNext(); )
        {
            Map.Entry entry = (Map.Entry) it.next();
            key = (String) entry.getKey();
            MultipartFile mvalue = (MultipartFile) entry.getValue();
            if ( isBindEmptyMultipartFiles() || !mvalue.isEmpty() )
            {
                parameters.put( key, mvalue );
            }
        }
        return parameters;
    }*/

    /**
     * ServletRequest 로부터 request 정보를 map 컬렉션으로 담는다. String[0] 은 String 타입으로
     * 변환하여 담는다.
     * 
     * <pre>
     * usage :
     * Map parameterMap = new BindUtils.requestToMap( request );
     * </pre>
     * 
     * @param request
     * @return
     */
    public HttpMap requestToMap( HttpServletRequest request )
    {
        HttpMap parameters = new RequestMap();
        String key = null;
        String[] value = null;

        	for ( Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements(); )
        	{
        		key = enumeration.nextElement().toString();
                value = request.getParameterValues( key );

                if ( value == null || value.length == 1 )
                {
                    parameters.put( key, value[0] );
                }
                else
                {
                    parameters.put( key, value );
                }
            }
   
        return parameters;
    }

    /**
     * ServletRequest 로부터 엄격한 타입 체크 request 정보를 map 컬렉션으로 담는다.
     * 
     * <pre>
     * usage :
     * Map parameterMap = new BindUtils.requestToMapStrict( request );
     * </pre>
     * 
     * @param request
     * @return
     */
    public HttpMap requestToMapStrict( HttpServletRequest request )
    {
        HttpMap parameters = new RequestMap();
   
        for ( Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements(); )
        {
        	String key = enumeration.nextElement().toString();
        	String[] value = request.getParameterValues( key );
            parameters.put( key, value );
        }
        
        return parameters;
    }

    public List requestToIBSheetMapList( HttpServletRequest request )
    {
        List mapList = new ArrayList();
        String sStatus[] = request.getParameterValues( "sStatus" );
        if ( sStatus != null )
        {
            for ( int i = 0; i < sStatus.length; i++ )
            {
                HttpMap parameters = new RequestMap();

                for ( Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements(); )
                {
                    String key = enumeration.nextElement().toString();
                    String value[] = request.getParameterValues( key );

                    if ( value != null )
                    {
                        if ( value.length > i )
                        {
                            parameters.put( key, value[i] );
                        }
                        else
                        {
                            parameters.put( key, value[0] );
                        }
                    }
                }
                mapList.add( parameters );
            }
        }
        return mapList;
    }

    public HttpMap requestMapToLogMap( HttpMap requestMap )
    {
        HttpMap LogMap = new RequestMap();

        if ( requestMap.isSetKeys( "req_no" ) )
        {
            if ( requestMap.get( "req_no" ) instanceof String )
            {
                LogMap.put( "req_no", (String) requestMap.get( "req_no" ) );
            }
            else
            {
                LogMap.put( "req_no", ( (String[]) requestMap.get( "req_no" ) )[0] );
            }
        }
        else
        {
            LogMap.put( "req_no", "" );
        }

        if ( requestMap.isSetKeys( "login_id" ) )
        {
            if ( requestMap.get( "login_id" ) instanceof String )
            {
                LogMap.put( "login_id", (String) requestMap.get( "login_id" ) );
            }
            else
            {
                LogMap.put( "login_id", ( (String[]) requestMap.get( "login_id" ) )[0] );
            }
        }
        else
        {
            LogMap.put( "login_id", "" );
        }

        if ( requestMap.isSetKeys( "log_detail" ) )
        {
            if ( requestMap.get( "log_detail" ) instanceof String )
            {
                LogMap.put( "log_detail", (String) requestMap.get( "log_detail" ) );
            }
            else
            {
                LogMap.put( "log_detail", ( (String[]) requestMap.get( "log_detail" ) )[0] );
            }
        }
        else
        {
            LogMap.put( "log_detail", "" );
        }

        return LogMap;
    }

    public HttpMap requestMapToImanMap( HttpMap requestMap )
    {
        HttpMap ImanMap = new RequestMap();

        if ( requestMap.isSetKeys( "req_no" ) )
        {
            if ( requestMap.get( "req_no" ) instanceof String )
            {
                ImanMap.put( "req_no", (String) requestMap.get( "req_no" ) );
            }
            else
            {
                ImanMap.put( "req_no", ( (String[]) requestMap.get( "req_no" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "req_no", "" );
        }

        if ( requestMap.isSetKeys( "req_title_name" ) )
        {
            if ( requestMap.get( "req_title_name" ) instanceof String )
            {
                ImanMap.put( "req_title_name", (String) requestMap.get( "req_title_name" ) );
            }
            else
            {
                ImanMap.put( "req_title_name", ( (String[]) requestMap.get( "req_title_name" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "req_title_name", "" );
        }

        if ( requestMap.isSetKeys( "cmplt_hope_day" ) )
        {
            if ( requestMap.get( "cmplt_hope_day" ) instanceof String )
            {
                ImanMap.put( "cmplt_hope_day", (String) requestMap.get( "cmplt_hope_day" ) );
            }
            else
            {
                ImanMap.put( "cmplt_hope_day", ( (String[]) requestMap.get( "cmplt_hope_day" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "cmplt_hope_day", "" );
        }

        if ( requestMap.isSetKeys( "project_id" ) )
        {
            if ( requestMap.get( "project_id" ) instanceof String )
            {
                ImanMap.put( "project_id", (String) requestMap.get( "project_id" ) );
            }
            else
            {
                ImanMap.put( "project_id", ( (String[]) requestMap.get( "project_id" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "project_id", "" );
        }

        if ( requestMap.isSetKeys( "alert_msg_cd" ) )
        {
            if ( requestMap.get( "alert_msg_cd" ) instanceof String )
            {
                ImanMap.put( "alert_msg_cd", (String) requestMap.get( "alert_msg_cd" ) );
            }
            else
            {
                ImanMap.put( "alert_msg_cd", ( (String[]) requestMap.get( "alert_msg_cd" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "alert_msg_cd", "" );
        }

        if ( requestMap.isSetKeys( "kt_coffr_emp_id" ) )
        {
            if ( requestMap.get( "kt_coffr_emp_id" ) instanceof String )
            {
                ImanMap.put( "kt_coffr_emp_id", (String) requestMap.get( "kt_coffr_emp_id" ) );
            }
            else
            {
                ImanMap.put( "kt_coffr_emp_id", ( (String[]) requestMap.get( "kt_coffr_emp_id" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "kt_coffr_emp_id", "" );
        }

        if ( requestMap.isSetKeys( "emp_no" ) )
        {
            if ( requestMap.get( "emp_no" ) instanceof String )
            {
                ImanMap.put( "emp_no", (String) requestMap.get( "emp_no" ) );
            }
            else
            {
                ImanMap.put( "emp_no", ( (String[]) requestMap.get( "emp_no" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "emp_no", "" );
        }

        if ( requestMap.isSetKeys( "analysys_user" ) )
        {
            if ( requestMap.get( "analysys_user" ) instanceof String )
            {
                ImanMap.put( "analysys_user", (String) requestMap.get( "analysys_user" ) );
            }
            else
            {
                ImanMap.put( "analysys_user", ( (String[]) requestMap.get( "analysys_user" ) )[0] );
            }
        }
        else
        {
            ImanMap.put( "analysys_user", "" );
        }

        return ImanMap;
    }

    public List MapToFileList( final Map map )
    {
        List mapList = new ArrayList();
        try
        {
            if ( map.get( "delete_file" ) != null )
            {
                String[] del_files = null;

                if ( map.get( "delete_file" ) instanceof String )
                {
                    del_files = new String[1];
                    del_files[0] = (String) map.get( "delete_file" );
                }
                else
                {
                    del_files = (String[]) map.get( "delete_file" );
                }

                for ( int i = 0; i < del_files.length; i++ )
                {
                    HashMap resultMap = new HashMap();
                    if ( del_files[i] != null && !"".equals( del_files[i] ) )
                    {
                        resultMap.put( "sStatus", "D" );
                        if ( map.get( "req_no" ) instanceof String )
                            resultMap.put( "req_no", map.get( "req_no" ) );
                        else
                            resultMap.put( "req_no", ( (String[]) map.get( "req_no" ) )[0] );
                        resultMap.put( "file_dstin_cd", map.get( "file_dstin_cd" ) );
                        //resultMap.put( "file_name", new String(del_files[i].getBytes("8859_1"),"utf-8") );
                        resultMap.put( "file_name", del_files[i] );
                        mapList.add( resultMap );
                    }
                }
            }

            if ( map.get( "upload_file" ) != null )
            {
                String[] up_files = null;

                if ( map.get( "upload_file" ) instanceof String )
                {
                    up_files = new String[1];
                    up_files[0] = (String) map.get( "upload_file" );
                }
                else
                {
                    up_files = (String[]) map.get( "upload_file" );
                }

                for ( int i = 0; i < up_files.length; i++ )
                {
                    HashMap resultMap = new HashMap();
                    if ( up_files[i] != null && !"".equals( up_files[i] ) )
                    {
                        resultMap.put( "sStatus", "I" );
                        if ( map.get( "req_no" ) instanceof String )
                            resultMap.put( "req_no", map.get( "req_no" ) );
                        else
                            resultMap.put( "req_no", ( (String[]) map.get( "req_no" ) )[0] );
                        resultMap.put( "file_dstin_cd", map.get( "file_dstin_cd" ) );
                        resultMap.put( "RealPath", map.get( "RealPath" ) );
                        resultMap.put( "seq_no", map.get( "seq_no" ) );
                        //resultMap.put( "file_name", new String(up_files[i].getBytes("8859_1"),"utf-8") );
                        resultMap.put( "file_name", up_files[i] );
                        mapList.add( resultMap );
                    }
                }
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            throw new RuntimeException( e.getMessage() );
        }
        return mapList;
    }
}
